package task.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.Map;

import android.os.Environment;
import android.util.Log;

/**
 * The user file storage class for the task manager.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 1.0
 */
public class LoginFacade {
	
	// User list of users.
	private UserList users;
	
	// Active User
	private String activeUser;
	
	// Instance that all classes will work with.
	private static LoginFacade INSTANCE;
	
	/**
	 * Returns an instance.
	 * 
	 * @return An instance.
	 */
	public static LoginFacade getInstance(){
		
		if(INSTANCE == null){
			INSTANCE = new LoginFacade();
		} // End of if statement.
		
		return INSTANCE;
	} // End of getInstance method.
	
	/**
	 * LoginFacade constructor.
	 */
	public LoginFacade(){
		
		try { // Try to load user datastructure from a file.
			loadUsers();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // End of try/catch statements.
		
		if(users == null){
			Map<String,String> tempMap = new HashMap<String,String>();
			tempMap.put("default", "default");
			users = new UserList(tempMap);
		} // End of if statement.
		
	} // End of constructor.
	
	/**
	 * Loads the users from a file.
	 * 
	 * @throws IOException Exception handling for storage file.
	 */
	public void loadUsers() throws IOException {
		ObjectInputStream ois = null;
		try {
			//we are using external storage, so we need to get the location
			//be sure you have the correct privileges 
			File file = new File(Environment.getExternalStorageDirectory() + "/login1.bin");
			ois = new ObjectInputStream(new FileInputStream(file));
			users = (UserList)ois.readObject();
		} catch (StreamCorruptedException e) {
			Log.e("APPLICATION FACADE", "Corrupted binary file");
		} catch (FileNotFoundException e) {
			Log.e("APPLICATION FACADE", "Binary File not found");
			System.out.println("Binary File not found");
		} catch (IOException e) {
			Log.e("APPLICATION FACADE", "General IO Error");
		} catch (ClassNotFoundException e) {
			Log.e("APPLICATION FACADE", "Class not found");
		} finally {
			if (ois != null)
				ois.close();
		} // End of try/catch/finally statements.
		
	} // End of loadUsers method.
	
	/**
	 * Saves the users to a file.
	 * 
	 * @throws IOException Exception handling for storage file.
	 */
	public void saveUsers() throws IOException {
		ObjectOutputStream oos = null;
		try {
			File file = new File(Environment.getExternalStorageDirectory() + "/login1.bin");
			//System.out.println("File created");
			oos = new ObjectOutputStream(new FileOutputStream(file));
			//System.out.println("ObjectOutputStream instantiated");
			oos.writeObject(users);
			//System.out.println("users object written to file");
			oos.flush();
			//System.out.println("objectOutputStream flushed");
		} catch (IOException e) {
			Log.e("TaskManagerActivity", "Binary file input general error");
			e.printStackTrace();
		} finally {
			if (oos != null) {
				oos.close();
			} // End of if statement.
		} // End of try/catch/finally statements.
	} // End of saveUsers method.
	
	/**
	 * Checks if the user and password match.
	 * 
	 * @param username User's name.
	 * @param password User's password.
	 * @return True if the user and password are registered and matched, false otherwise.
	 */
	public boolean isValid(String username, String password) {
		return users.isValid(username, password);
	} // End of isValid method.
	
	/**
	 * Registers a new user and saves their name and password.
	 * 
	 * @param username User's name.
	 * @param password User's password.
	 */
	public void registerNewUser(String username, String password) {
		users.registerNewUser(username, password);
	} // End of registerNewUser method.
	
	public String getActiveUser(){
		return activeUser;
	}
	
	public void setActiveUser(String activeUser){
		this.activeUser = activeUser;
	}
	
} // End of LoginFacade class.
